/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchsachxuatbanException;
import doan.quanlycanbo.portlet.model.sachxuatban;
import doan.quanlycanbo.portlet.service.base.sachxuatbanLocalServiceBaseImpl;

/**
 * 
 * @author Nguyen Tuan Vinh
 */
public class sachxuatbanLocalServiceImpl extends sachxuatbanLocalServiceBaseImpl {
	
	public List<sachxuatban> getAll() throws SystemException{
		return sachxuatbanPersistence.findAll();
	}
	
	public sachxuatban addSachXuatBan(sachxuatban sachxuatban1, long userId) throws SystemException{
		long masxb = CounterLocalServiceUtil.increment(sachxuatban.class.getName());
		sachxuatban sachxuatban2 = sachxuatbanPersistence.create(masxb);
		sachxuatban2.setGroupId(sachxuatban1.getGroupId());
		sachxuatban2.setCompanyId(sachxuatban1.getCompanyId());
		sachxuatban2.setLanguageId(sachxuatban1.getLanguageId());
		sachxuatban2.setTensach(sachxuatban1.getTensach());
		sachxuatban2.setMahoso(sachxuatban1.getMahoso());
		sachxuatban2.setNamxb(sachxuatban1.getNamxb());
		return sachxuatbanPersistence.update(sachxuatban2, false);
	}
	
	public sachxuatban updateSachXuatBan(long masachxuatban, String tensxb, long maHoso, int namXB) throws SystemException, NoSuchsachxuatbanException{
		sachxuatban sachxuatban = sachxuatbanPersistence.findByPrimaryKey(masachxuatban);
		sachxuatban.setTensach(tensxb);
		sachxuatban.setMahoso(maHoso);
		sachxuatban.setNamxb(namXB);
		return sachxuatbanPersistence.update(sachxuatban, false);
	}
	
	public sachxuatban deleteSachXuatBan(long masachxuatban) throws SystemException, NoSuchsachxuatbanException{
		sachxuatban sachxuatban = sachxuatbanPersistence.findByPrimaryKey(masachxuatban);
		return deleteSachXuatBan(sachxuatban);
	}
	
	public sachxuatban deleteSachXuatBan(sachxuatban sachxuatban) throws SystemException{
		return sachxuatbanPersistence.remove(sachxuatban);
	}
	
	public sachxuatban addSXB(sachxuatban sxb1, long userId) throws SystemException{
		long masxb = CounterLocalServiceUtil.increment(sachxuatban.class.getName());
		
		sachxuatban sxb2 = sachxuatbanPersistence.create(masxb);		
		sxb2.setGroupId(sxb1.getGroupId());
		sxb2.setCompanyId(sxb1.getCompanyId());
		sxb2.setLanguageId(sxb1.getLanguageId());
		sxb2.setMahoso(sxb1.getMahoso());
		sxb2.setTensach(sxb1.getTensach());
		sxb2.setNamxb(sxb1.getNamxb());
		try {
			resourceLocalService.addResources(sxb1.getCompanyId(), sxb1.getGroupId(), userId, 
					sachxuatban.class.getName(), sxb2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return sachxuatbanPersistence.update(sxb2, false);
	}
	
	public sachxuatban updateSXB(long masxb,String tensach, int namxb) throws SystemException, NoSuchsachxuatbanException{
		sachxuatban sxb = sachxuatbanPersistence.findByPrimaryKey(masxb);
		sxb.setTensach(tensach);
		sxb.setNamxb(namxb);
		return sxb;
	}
	
	public void deleteSXB(long masxb) throws SystemException, PortalException{
		sachxuatban sxb = sachxuatbanPersistence.findByPrimaryKey(masxb);
		deleteSXB(sxb);
	}
	
	public void deleteSXB(sachxuatban sxb) throws PortalException, SystemException{
		resourceLocalService.deleteResource(sxb.getCompanyId(), sachxuatban.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, sxb.getPrimaryKey());
		sachxuatbanPersistence.remove(sxb);
	}
}